<?php
/**
 * 发票处理类
 *
 * @author sunjing
 * 
 * @version 0.1
 */
class invoice_order
{
 
    public function create($invoice_data,$msg){

        $invoiceObj = app::get('invoice')->model('axorder');
       
        $operator = kernel::single('desktop_user')->get_name();

        $dateline =preg_replace( array('/年/','/月/','/日/'), array('-','-',''), $invoice_data['dateline']);
        $insertData = array(
           
            'order_id'       => $invoice_data['order_id'],
            'order_bn'       => $invoice_data['order_bn'],
            'mode'           => 1,//这个key不存在就是0，1为电子发票
            'amount'         => $invoice_data['amount'],
            'cost_tax'       => $invoice_data['cost_tax'],
            'tax_amount'    =>  $invoice_data['tax_amount'],
            'invoice_code'  =>  $invoice_data['invoice_code'],
            'operator'       => $operator ?$operator : 'system',
            'create_time'    => time(),
            'tax_company'    => $invoice_data['tax_company'],
            'ship_tax'       =>  $invoice_data['ship_tax'],
            'invoice_no'    =>  $invoice_data['invoice_no'],

            'ship_addr'      => $invoice_data['ship_addr'],
            'ship_tel'       => $invoice_data['ship_tel'],
            'shop_id'        => $invoice_data['shop_id'],
            'payee_name'     => $invoice_data['payee_name'],
            'tax_no'         => $invoice_data['tax_no'],
            'address'        => $invoice_data['address'],

            'payee_operator' => $invoice_data['payee_operator'],
            'bank'           => $invoice_data['bank'],
            'dateline'      =>  strtotime($dateline),
          
            'create_time'   =>  time(),
            'billing_type'  =>  $invoice_data['billing_type'],
            'org_invoice_code'=>    $invoice_data['org_invoice_code'],
            'org_invoice_no'    =>  $invoice_data['org_invoice_no'],
            'url'           =>  $invoice_data['url'],
            'serial_no'     =>  $invoice_data['serial_no'],
            
        );

        $rs = app::get('invoice')->model('axorder')->insert($insertData);

        return true;
    }   




    public function requestBilling($id,$err_msg){

        
        $tax_params = $this->get_tax_params($id);



    }

    public function get_tax_params($id){

        $invoiceObj = app::get('invoice')->model('axorder');
        $invoice_detail = $invoiceObj->dump(array('id'=>$id),'*');
        $order_id = $invoice_detail['order_id'];
        $data = array(
            'invoiceNum'        =>  $invoice_detail['invoice_no'],
            'invoiceDate'       =>   date('Y-m-d',$invoice_detail['dateline']),

            'clerk'             =>  $invoice_detail['payee_operator'],
           
            'invoiceType'       =>  $invoice_detail['billing_type'],
            'invoiceLine'       =>  1,
            'remark'            =>  '',
           
        );

        //invoiceCode
        if ($invoice_detail['billing_type'] == '2'){
            $data['invoiceCode'] = $invoice_detail['org_invoice_no'];
        }
        $item_list = $this->get_itemsByorder_id($order_id);
        $taxdetail = array();
        foreach($item_list as $iv){
            $taxdetail[] = array(
                'SalesId'           =>  $invoice_detail['order_bn'],
                'packingslipId'     =>  $iv['axshipno'],
                'goodsCode'         =>  $iv['bn'],
                'goodsName'         =>  $iv['product_name'],
                'InvoiceQty'        =>  $iv['number'],
                'InvoiceUnit'       =>  '',
                'Price'             =>  $iv['price'],
                'Tax'               =>  '',
                'taxIncludedAmount' =>  $iv['amount'],
                'taxExcludedAmount' =>  '',
                'taxRate'           =>  '',
            );
        }
        
        return $data;
    }

    



    public function get_itemsByorder_id($order_id){

        $db = kernel::database();
        $sql = "SELECT od.delivery_bn,od.delivery_id FROM   sdb_ome_delivery od LEFT JOIN sdb_ome_delivery_order odo ON  od.delivery_id = odo.delivery_id WHERE odo.order_id=".$order_id." AND od.status in ('succ')";
         
       
       $delivery_list = $db->select($sql);

       $delivery_bns = array();
       $delivery_ids = array();

       foreach($delivery_list as $delivery){
            $delivery_bns[$delivery['delivery_id']] = $delivery['delivery_bn'];
           
       }

       $sql1 = "SELECT dli.delivery_bn,dli.axshipno,dli.bnsContent,e.outer_delivery_bn from  sdb_wms_delivery e 
         LEFT join sdb_wms_deliverynote_line dli on dli.delivery_bn=e.delivery_bn
            where e.outer_delivery_bn in ('".implode('\',\'',array_values($delivery_bns))."')";
            
        $_rows = $db->select($sql1);

        $delivery_lines = array();
       
        foreach($_rows as $v){
            $delivery_lines[$v['outer_delivery_bn']][$v['bnsContent']] = $v['axshipno'];

        }


       $items_list = $db->select("SELECT * FROM sdb_ome_delivery_items WHERE delivery_id in(".implode(',',array_keys($delivery_bns)).")");

        foreach($items_list as &$v){
            $delivery_bn = $delivery_bns[$v['delivery_id']];
            //Price packingslipId
            $bn = $v['bn'];

            $number = $v['number'];

            $item_id = $v['item_id'];
            //
            $items_detail = $db->selectrow("SELECT order_item_id,order_obj_id FROM sdb_ome_delivery_items_detail WHERE delivery_item_id=".$item_id);
            $order_item_id = $items_detail['order_item_id'];
            $order_obj_id = $items_detail['order_obj_id'];

            $order_items = $db->selectrow("SELECT divide_order_fee,nums FROM sdb_ome_order_items WHERE item_id=".$order_item_id." AND obj_id=".$order_obj_id);

            $price = sprintf('%.2f',$order_items['divide_order_fee']/$order_items['nums']);

            $v['price'] = $price;
            $v['amount'] = sprintf('%.2f',$v['price']*$v['number']);
            $axshipno = $delivery_lines[$delivery_bn][$v['bn']];
            $v['axshipno'] = $axshipno;

        }


        return $items_list;

    }
    
}